package com.example.mymvp02.Base;

import android.app.AlertDialog;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.mymvp02.Constants.BroadCastReceiverAction;
import com.example.mymvp02.LoginMvp.LoginActivity;
import com.example.mymvp02.MyApplication;

/**
 * @user XUNUA
 * @email xunua@outlook.com
 * @creat time 2020/4/14 16:27
 */
public abstract class BaseActivity<P extends BasePresenter,CONTRACT> extends AppCompatActivity implements View.OnClickListener {
    public abstract CONTRACT getContract();

    public P mPresenter;

    public MyApplication myApplication;

    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContentView();
        initView();
        initData();
        initListener();
        mPresenter=getPresenterInstance();
        mPresenter.bindView(this);
        myApplication= (MyApplication) getApplication();
    }

    public abstract void initContentView();//setContentView(R.layout.activity_main);


    public abstract void initView();//绑定组件

    public abstract void initData();//数据绑定

    public abstract void initListener();//设置监听

    @Override
    protected void onDestroy() {
        super.onDestroy();
        destroy();
    }

    public abstract void destroy();

    public abstract P getPresenterInstance();

    @Override
    protected void onResume() {
        super.onResume();
        //接收下线广播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BroadCastReceiverAction.ActionUserOut);//接收下线广播
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, Intent intent) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setCancelable(false);
                builder.setTitle("Warning").setMessage("账号状态异常，即将下线").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        myApplication.finishAllActivity();//销毁所有activity
                        unregisterReceiver(receiver);
                        //可以使用intent来打开用户登陆界面
                        Intent intentLogin = new Intent(context, LoginActivity.class);
                        startActivity(intentLogin);
                    }
                }).show();
            }
        };
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
